# -*- coding: utf-8 -*-
__author__ = 'Liantian'


from django.core.files.storage import FileSystemStorage
from django.conf import settings

class ImageStorage(FileSystemStorage):
    # 修改上传文件名
    # edit : setting.py
    # DEFAULT_FILE_STORAGE = 'core.ImageStorage'

    def __init__(self, location=settings.MEDIA_ROOT, base_url=settings.MEDIA_URL):
    # 初始化
        super(ImageStorage, self).__init__(location, base_url)

        # 重写 _save方法
    def _save(self, name, content):
        import os, time
        #文件扩展名
        ext = os.path.split(name)[-1]
        #文件目录
        d = os.path.dirname(name)
        #定义文件名，年月日时分秒随机数
        fn = time.strftime('%d%H%M%S_')
#        fn = fn + '%d' % random.randint(0,100)
        #重写合成文件名
        name = os.path.join(d, fn + ext)
        #调用父类方法
        return super(ImageStorage, self)._save(name, content)